/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <kernel/initcall.h>
#include <kernel/syslog.h>


extern void eb_sys_exit(int state);


extern initcall_t __eb_early_initcall_start[];
extern initcall_t __eb_early_initcall_end[];


/**
 * all initcall symbol, it will be in .EB_initcall0,1,2... section
 */
extern initcall_t __eb_initcall0_start[];
extern initcall_t __eb_initcall1_start[];
extern initcall_t __eb_initcall2_start[];
extern initcall_t __eb_initcall3_start[];
extern initcall_t __eb_initcall4_start[];
extern initcall_t __eb_initcall5_start[];
extern initcall_t __eb_initcall6_start[];
extern initcall_t __eb_initcall7_start[];
extern initcall_t __eb_initcall_end[];


/**
 * Initialize Table, Initialize the table, which facilitates traversing all initialization symbols
 */
static initcall_t *initcall_levels[] = {
    __eb_initcall0_start,
    __eb_initcall1_start,
    __eb_initcall2_start,
    __eb_initcall3_start,
    __eb_initcall4_start,
    __eb_initcall5_start,
    __eb_initcall6_start,
    __eb_initcall7_start,
    __eb_initcall_end,
};


/**
 * Name of initialization level
 */
static const char initcall_level_names[][7] = {
    "base",
    "core",
    "arch",
    "fs",
    "bus",  
    "device",
    "driver",
    "module",
    "end",
};


/**
 * @brief To initiate a specified level initcall
 * @param level: level of initcall
 * @return none
 */
static void do_initcall_level(int level)
{
    initcall_t *fn;
    const char *level_name = initcall_level_names[level];
    int ret = -1;

    for (fn = initcall_levels[level]; fn < initcall_levels[level + 1]; fn ++) {
        ret = (*fn)();

        if(ret != 0) {
            SYSLOG_ERROR("level[%s] initcall error, symbol address: %x", level_name, (*fn - 1));
            eb_sys_exit(1);
        }
    }
}


/**
 * @brief To callback all initcall function
 * @param none
 * @return none
 */
void do_initcalls(void)
{
    for (unsigned int level = 0; level < ARRAY_SIZE(initcall_levels) - 1; level++) {
        do_initcall_level(level);
    }
}


/**
 * @brief To callback early initcall function
 * @param none
 * @return none
 */
void do_early_initcalls(void)
{
    initcall_t *fn;
    int ret = -1;

    for (fn = __eb_early_initcall_start; fn < __eb_early_initcall_end; fn ++) {
        ret = (*fn)();

        if(ret != 0) {
            SYSLOG_ERROR("early initcall error, symbol address: %x", (int*)fn);
            eb_sys_exit(1);
        }
    }
}
